//
//  CYYNetWorkManager+CommentAParty.m
//  LetsPartyBmob
//
//  Created by rimi on 15/9/9.
//  Copyright (c) 2015年 柴阳阳. All rights reserved.
//

#import "CYYNetWorkManager+CommentAParty.h"

@implementation CYYNetWorkManager (CommentAParty)
//评论一个活动
//此操作在活动详情界面操作
/*
 partyObjectId : 上一个界面推送 过程中 传值过来的。。
 comment: 评论内容
 */
//+ (void)commentAPartyWithObjectId:(NSString *)partyObjectId
//                          comment:(NSString *)comment
//                           sucess:(Sucess)sucess
//                             fail:(Fail)fail{
//    //取到改活动 对象
//    BmobObject *bmobObject = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
//    //创建评论 对象
//    BmobObject * commentObject = [BmobObject objectWithClassName:@"Comments"];
//    //评论：评论内容+评论对象
//    [commentObject setObject:comment forKey:@"commentContent"];
//    [commentObject setObject:[CYYNetWorkManager currentUserGet] forKey:@"commentAuthor"];//pointer关系
//    [commentObject saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
//        if (isSuccessful) {
//            //新建relation对象
//            BmobRelation *relation = [[BmobRelation alloc] init];
//            [relation addObject:[BmobObject objectWithoutDatatWithClassName:@"Comments" objectId:commentObject.objectId]];
//            
//            [bmobObject addRelation:relation forKey:@"partyComments"];
//            
//            [bmobObject updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
//                if (isSuccessful) {
//                    sucess(@"评论成功");
//                }else{
//                    fail(error);
//                }
//            }];
//
//        }else{
//            fail(error);
//        }
//    }];
//}

+ (void)commentAPartyWithObjectId:(NSString *)partyObjectId
                          comment:(NSString *)comment
                           sucess:(Sucess)sucess
                             fail:(Fail)fail{
    //取到改活动 对象
    BmobObject *bmobObject = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
    //创建评论 对象
    BmobObject * commentObject = [BmobObject objectWithClassName:@"Comments"];
    //评论：评论内容+评论对象
    [commentObject setObject:comment forKey:@"commentContent"];
    //评论者的名字
    [commentObject setObject:[[CYYNetWorkManager currentUserGet] objectForKey:@"name"] forKey:@"commentAuthorName"];
    [commentObject saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            //新建relation对象
            BmobRelation *relation = [[BmobRelation alloc] init];
            [relation addObject:[BmobObject objectWithoutDatatWithClassName:@"Comments" objectId:commentObject.objectId]];
            
            [bmobObject addRelation:relation forKey:@"partyComments"];
            
            [bmobObject updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                if (isSuccessful) {
                    sucess(@"评论成功");
                }else{
                    fail(error);
                }
            }];
            
        }else{
            fail(error);
        }
    }];
}
/*
 先获取活动详情--》取下来评论列表数组--》然后把评论插在数组的第一位的位置。
 */
+ (void)newCommentAPartyWithObjectId:(NSString *)partyObjectId
                          comment:(NSString *)comment
                           sucess:(Sucess)sucess
                             fail:(Fail)fail{
    //取到改活动 对象
    BmobObject *bmobObject = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
    __block NSMutableArray * mArray = [NSMutableArray array];//接受评论数组
    /**< 查询活动评论数组 */
    [CYYNetWorkManager partyDetailWithObjectId:partyObjectId sucess:^(BmobObject *object) {
       //活动成功
        mArray = [[object objectForKey:@"partyCommentsArray"] mutableCopy];
        if (mArray.count == 0) {
            //直接评论
            [CYYNetWorkManager hidenCommentAPartyWithObjectId:partyObjectId comment:comment sucess:^(NSString *sucessString) {
//                NSLog(@"评论成功");
            } fail:^(NSError *error) {
                
            }];
        }else{
            [mArray insertObject:@[@{@"commentAuthorName":[[CYYNetWorkManager currentUserGet] objectForKey:@"name"],
                                     @"commentContent":comment,
                                     @"createdAt":[self stringFromDate:[NSDate date]]}] atIndex:0];
            
            [bmobObject setObject:mArray forKey:@"partyCommentsArray"];
            
            [bmobObject updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                if (isSuccessful) {
                    sucess(@"评论成功");
                }else{
                    fail(error);
                }
            }];
        }
    } fail:^(NSError *error) {
        
    }];
}


+ (void)hidenCommentAPartyWithObjectId:(NSString *)partyObjectId
                          comment:(NSString *)comment
                           sucess:(Sucess)sucess
                             fail:(Fail)fail{
    //取到改活动 对象
    BmobObject *bmobObject = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
    //创建评论 对象
    BmobObject * commentObject = [BmobObject objectWithClassName:@"Comments"];
    //评论：评论内容+评论对象
    [commentObject setObject:comment forKey:@"commentContent"];
    //评论者的名字
    [commentObject setObject:[[CYYNetWorkManager currentUserGet] objectForKey:@"name"] forKey:@"commentAuthorName"];
    [commentObject saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            //新建relation对象
            BmobRelation *relation = [[BmobRelation alloc] init];
            [relation addObject:[BmobObject objectWithoutDatatWithClassName:@"Comments" objectId:commentObject.objectId]];
            
            [bmobObject addRelation:relation forKey:@"partyComments"];
            
            [bmobObject setObject:@[@{@"commentAuthorName":[[CYYNetWorkManager currentUserGet] objectForKey:@"name"],
                                      @"commentContent":comment,
                                      @"createdAt":[self stringFromDate:[NSDate date]]}] forKey:@"partyCommentsArray"];
            
            [bmobObject updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                if (isSuccessful) {
                    sucess(@"评论成功");
                }else{
                    fail(error);
                }
            }];
            
        }else{
            fail(error);
        }
    }];
}

@end
